# import jieba

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pandas as pd

import wordcloud #导入词云库
# https://blog.csdn.net/moshanghuali/article/details/84667136
# SIMYOUTTF_downcc


# data=pd.read_csv("动漫评价数据集/anime_t.csv",encoding="gbk")
# data=pd.read_csv("bangumi_csv/bangumi_1.csv",encoding="gbk")
# seg_list = jieba.cut("我来到北京清华大学", cut_all = True)
# print("Full Mode:", ' '.join(seg_list))
def join_all_data():
    tot_num=49
    datas=[]
    for num in range(1,tot_num+1):
        try:
            datas.append(pd.read_csv("bangumi_csv/bangumi_{}.csv".format(num),encoding="gbk"))
        except:
            datas.append(pd.read_csv("bangumi_csv/bangumi_{}.csv".format(num),encoding="utf-8"))
    # data=datas[0]
    data= pd.DataFrame()
    for num in range(tot_num):
        data=data.append(datas[num],ignore_index=True)

    return data

def push_list(lst,creators):
    creators=creators.split("/")
    for crea in creators:
        creator=crea.strip()
        if creator=="创作者不明":
            continue
        lst.append(creator)
        
        
def split_creators(data):
    creators_list=[]
    for creators in data["创作者"]:
        push_list(creators_list,creators)
    return creators_list

def list_to_blank_words(lst):
    res=""
    for w in lst:
        if w=="创作者不明" or w=="連載":
            continue
        res+=" "+w
    return res

def gen_dic_frec(lst):
    dic_frec={}
    for sth in lst:
        if sth not in dic_frec:
            dic_frec[sth]=1
        else:
            dic_frec[sth]+=1

    return dic_frec

data=join_all_data()
# r"D:\project\jsProject\animals\fonts\fontawesome-webfont.ttf"
WC = wordcloud.WordCloud(font_path ="SIMYOUTTF_downcc/SIMYOU.TTF" ,
                         max_words=100,height= 800,width=800,background_color='white',repeat=False,mode='RGBA')
#设置词云图对象属性
# https://blog.csdn.net/weixin_42240667/article/details/104955036

# ————————————————
# 版权声明：本文为CSDN博主「moshanghuali」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
# 原文链接：https://blog.csdn.net/moshanghuali/article/details/84667136
# conten = ' '.join(jieba.lcut(st1)) #此处分词之间要有空格隔开，联想到英文书写方式，每个单词之间都有一个空格。
creators=split_creators(data)
# print(creators)
# words_list=list_to_blank_words(creators)
# print(words_list)

plt.rcParams['font.sans-serif']=[u'SimHei']

# conten = words_list
# con = WC.generate(conten)
dic_frec=gen_dic_frec(creators)
# print(dic_frec)


con = WC.generate_from_frequencies(dic_frec)
# https://www.cnblogs.com/liangmingshen/p/11312257.html
plt.imshow(con)
plt.axis("off")
# https://blog.csdn.net/moshanghuali/article/details/84667136
# 词云图 都是 方框
plt.show()